package ajax.servlet;

import ajax.entity.User;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

public class CheckUserServlet2 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收jquery发送的ajax数据
        String username = req.getParameter("username");

        //设置text/json,不然他会优先匹配text/html导致出错
        resp.setContentType("text/json;charset=utf-8");
        Gson gson = new Gson();
        if("king".equals(username)){
            User user = new User(100, "king", "abc", "king@sohu.com");
            //返回数据
            resp.getWriter().write(gson.toJson(user));
        }else {
            //返回一个不存在的User
            User user = new User(-1, "", "", "");
            resp.getWriter().write(gson.toJson(user));
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
